package auction.chapter16.cmd;

import java.math.BigDecimal;

import auction.chapter16.BusinessException;
import auction.chapter16.dao.ItemDAO;
import auction.chapter16.dao.UserDAO;
import auction.model.Bid;
import auction.model.Item;

public class BidForItemCommand extends DataAccessCommand {
	
	//Input
	private Long userId;
	private Long itemId;
	private BigDecimal bidAmount;
	
	//Output
	private Bid newBid;
	
	public BidForItemCommand(Long userId,Long itemId,BigDecimal bidAmount){
		this.userId=userId;
		this.itemId=itemId;
		this.bidAmount=bidAmount;
	}
	
	public Bid getNewBid(){
		return newBid;
	}

	public void execute() throws CommandException {
		ItemDAO itemDAO=daoFactory.getItemDAO();
		UserDAO userDAO=daoFactory.getUserDAO();
		
		try{
			Bid currentMaxBid=itemDAO.getMaxBid(itemId);
			Bid currentMinBid=itemDAO.getMinBid(itemId);
			
			Item item=itemDAO.findById(itemId, false);
			
			newBid=item.placeBid(userDAO.findById(userId, false), bidAmount, currentMaxBid, currentMinBid);
		}catch(BusinessException ex){
			throw new CommandException(ex);
		}
	}

}
